/*----------------------------------------------------------
* FILE: JasperFormat.java
* LOGICAL NAME: JasperFormat
* PRODUCT: bila-framework
*----------------------------------------------------------
* MODIFICATIONS
* DATE Oct 10, 2011
* AUTHOR wjuarez
*----------------------------------------------------------
*/

package bila.framework.reports.jasper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;

/**
 * @author WJuarez
 * <b>Created by </b>wjuarez
 * <b>for</b> bila-framework
 * <b>package</b> bila.framework.reports.jasper
 * <b>on</b> Oct 10, 2011 9:45:23 PM
 * <b>Purpose</b> 
 * <p>
 *		Funciones de apoyo para la generacion de reportes con JasperReports
 *		en formato PDF, Excel, HTML, CSV, RTF
 * </p>
 */
public class JasperFormat {

	/**
	 * Genera el reporte en formato PDF
	 * @param response HttpServletResponse
	 * @param jasperPrint Objeto Jasper a imprimir
	 * @param nameReport nombre del reporte
	 * @throws Exception
	 */
	protected void generatePdfReport(HttpServletResponse response, JasperPrint jasperPrint, String nameReport) throws Exception{
		byte[] bytes = null;
		try {						
			bytes = JasperExportManager.exportReportToPdf(jasperPrint);
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition", "inline;filename=" + nameReport + ".pdf");
			response.setHeader("Cache-Control", "max-age=30");
			response.setHeader("Pragma", "No-cache");
			response.setDateHeader("Expires", 0);
			response.setContentLength(bytes.length);
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(bytes, 0, bytes.length);
			ouputStream.flush();
			ouputStream.close();
		} catch (Exception e) {
			throw new Exception("Problemas con la exportacion a pdf, Jasper Exception: "+ e.getMessage());
		}
	}
	
	/**
	 * Genera el reporte en formato HTML
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @param jasperPrint Objeto Jasper a imprimir
	 * @throws Exception
	 */
	protected void generateHtmlReport(HttpServletRequest request, HttpServletResponse response, JasperPrint jasperPrint) throws Exception{
		JRHtmlExporter exporter = new JRHtmlExporter();
		try {
			PrintWriter out = response.getWriter();
			request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
			exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "ISO-8859-1");
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
			exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
			exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"image?image="); 
			exporter.exportReport();
		} catch (Exception e) {
			throw new Exception("Problemas con la exportacion a Html, Jasper Exception: " + e);
		}
	}
	
	/**
	 * Genera el reporte en formato CSV
	 * @param response HttpServletResponse
	 * @param jasperPrint Objeto jasper a imprimir
	 * @param nameReport Nombre del reporte
	 * @throws Exception
	 */
	protected void generateCsvReport(HttpServletResponse response, JasperPrint jasperPrint, String nameReport) throws Exception{
		JRCsvExporter exporter = new JRCsvExporter();
		ByteArrayOutputStream csvReport = new ByteArrayOutputStream();
		try {
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, csvReport);
			exporter.exportReport();
		} catch (JRException jex) {
			throw new Exception("Problemas con la exportacion a excel, Jasper Exception: " + jex);
		}
		byte[] bytes = csvReport.toByteArray();
		response.setContentType("text/csv");
		response.setContentLength(bytes.length);
		response.setHeader("Content-Disposition", "attachment;filename=" + nameReport + ".csv");

		try {
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(bytes, 0, bytes.length);
			ouputStream.flush();
			ouputStream.close();
		} catch (IOException ioex) {
			throw new Exception("Problemas con la exportacion a excel, en el outPutStream : " + ioex);
		}
	}
	
	/**
	 * Genera el reporte en formato XLS
	 * @param response HttpServletResponse
	 * @param jasperPrint Objeto jasper a imprimir
	 * @param nameReport Nombre del reporte
	 * @throws Exception
	 */
	protected void generateXlsReport(HttpServletResponse response, JasperPrint jasperPrint, String nameReport) throws Exception {
		JRXlsExporter exporter = new JRXlsExporter();
		ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
		try {
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
			exporter.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
			exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
      	    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
      	    
			exporter.exportReport();
		} catch (JRException jex) {
			throw new Exception("Problemas con la exportacion a excel, Jasper Exception: " + jex);
		}

		byte[] bytes = xlsReport.toByteArray();
		response.setContentType("application/vnd.ms-excel");
		response.setContentLength(bytes.length);
		response.setHeader("Content-Disposition", "attachment;filename=" + nameReport + ".xls");

		try {
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(bytes, 0, bytes.length);
			ouputStream.flush();
			ouputStream.close();
		} catch (IOException ioex) {
			throw new Exception("Problemas con la exportacion a excel, en el outPutStream :  "+ ioex);
		}
	}
	
	/**
	 * Genera el reporte en formato RTF
	 * @param response HttpServletResponse
	 * @param jasperPrint Objeto jasper a imprimir
	 * @param nameReport Nombre del reporte
	 * @throws Exception
	 */
	protected void generateRtfReport(HttpServletResponse response, JasperPrint jasperPrint, String nameReport) throws Exception {
		JRRtfExporter exporter = new JRRtfExporter();
		response.setContentType("application/rtf");
		response.setHeader("Content-Disposition", "attachment;filename="+ nameReport + ".rtf");

		PrintWriter out = response.getWriter();
		try {
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
			exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, nameReport + ".rtf");
			exporter.exportReport();
		} catch (JRException jex) {
			throw new Exception("Problemas con la exportacion a rtf, Jasper Exception: " + jex);
		}

	}
}
